//
//  APIRequester.h
//  VFA_QUEUENCE
//
//  Created by Phan Ba Minh on 2/5/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Define.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "ASINetworkQueue.h"
#import "ASIS3Request.h"
#import "ASIS3ObjectRequest.h"

#ifdef __DEBUG__

//#define STRING_REQUEST_ROOT                                                 @"http://localhost:1337"
//#define STRING_REQUEST_ROOT                                                 @"http://phongdep.hp.af.cm"
#define STRING_REQUEST_ROOT                                                 @"http://phongdep.ap01.aws.af.cm"

#else

//#define STRING_REQUEST_ROOT                                                 @"http://localhost:1337"
//#define STRING_REQUEST_ROOT                                                 @"http://phongdep.hp.af.cm"
#define STRING_REQUEST_ROOT                                                 @"http://phongdep.ap01.aws.af.cm"

#endif


#define STRING_REQUEST_SERCRET_KEY @"3eac3f982fd8d14919e92df28efcf0481b294a3efe4e7a987a1ab74ce1d1df80aacf02b57ea1f51057ffae73b9b60b9811eb990459b6c2b7777c0432306fb291"
#define TIMER_REQUEST_TIMEOUT                                               60


#define SYSTEM_VERSION_EQUAL_TO(v)                  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame)
#define SYSTEM_VERSION_GREATER_THAN(v)              ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending)
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN(v)                 ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
#define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v)     ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending)


// *************************************REQUEST**********************************
// USER
#define STRING_REQUEST_URL_USER_LOGIN                                   [NSString stringWithFormat:@"%@/accounts/login", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_LOGOUT                                  [NSString stringWithFormat:@"%@/accounts/logout", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_REGISTRY_ACCOUNT                             [NSString stringWithFormat: @"%@/accounts/signup", STRING_REQUEST_ROOT]

#define STRING_REQUEST_URL_USER_LOGIN_USERNAME_EMAIL                    [NSString stringWithFormat:@"%@/accounts/login/email/username", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_UPDATE_LOCATION_AND_GPS                 [NSString stringWithFormat:@"%@/accounts/location", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_CHANGE_PASSWORD                         [NSString stringWithFormat:@"%@/accounts/change/password", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_RECOVERY_PASSWORD                       [NSString stringWithFormat:@"%@/accounts/forget/password/user", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_FORGET_PASSWORD                         [NSString stringWithFormat:@"%@/accounts/recovery/forget/password", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_UPDATE_DEVICE_UDID                      [NSString stringWithFormat:@"%@/account/update/deviceudid", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_UPDATE_DEVICE_TOKEN                     [NSString stringWithFormat:@"%@/account/update/devicetoken", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_UPDATE_INFO                             [NSString stringWithFormat:@"%@/account/update/userinfo", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_USER_UPDATE_NOTIFICATION_CENTER              [NSString stringWithFormat:@"%@/account/update/notificationcenter", STRING_REQUEST_ROOT]

#define STRING_REQUEST_URL_USER_UPDATE_COUNTRY_CODE                     [NSString stringWithFormat:@"%@/account/update/countrycode", STRING_REQUEST_ROOT]

// ACTIONS
#define STRING_REQUEST_URL_ACTIONS_ENTER_FOREGROUND_APP                 [NSString stringWithFormat:@"%@/actions/enter_foreground_app", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_ACTIONS_CONTACT_BY_EMAIL_01                  [NSString stringWithFormat:@"%@/actions/contact_by_email_01", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_ACTIONS_OPEN_URL_01                          [NSString stringWithFormat:@"%@/actions/open_url_01", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_ACTIONS_CALL_PHONE_01                        [NSString stringWithFormat:@"%@/actions/call_phone_01", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_ACTIONS_CALL_PHONE_02                        [NSString stringWithFormat:@"%@/actions/call_phone_02", STRING_REQUEST_ROOT]
#define STRING_REQUEST_URL_ACTIONS_VIEW_ROOM_INFO                       [NSString stringWithFormat:@"%@/actions/view_room_info", STRING_REQUEST_ROOT]

// PHONGDEP
#define STRING_REQUEST_PHONGDEP_ROOT                                    @"http://phongdep.vn/ws"
#define STRING_REQUEST_URL_PHONGDEP_GET_ROOMS(offset, limit)            [NSString stringWithFormat:@"%@/free_rooms?secret_key=%@&offset=%d&limit=%d", STRING_REQUEST_PHONGDEP_ROOT, STRING_REQUEST_SERCRET_KEY, offset, limit]
#define STRING_REQUEST_URL_PHONGDEP_GET_HOUSES(offset, limit)           [NSString stringWithFormat:@"%@/house_list?secret_key=%@&offset=%d&limit=%d", STRING_REQUEST_PHONGDEP_ROOT, STRING_REQUEST_SERCRET_KEY, offset, limit]
#define STRING_REQUEST_URL_PHONGDEP_GET_ROOM_DETAIL(room_id)            [NSString stringWithFormat:@"%@/room?roomid=%@&secret_key=%@", STRING_REQUEST_PHONGDEP_ROOT, room_id, STRING_REQUEST_SERCRET_KEY]
#define STRING_REQUEST_URL_PHONGDEP_GET_HOUSE_DETAIL(house_id)           [NSString stringWithFormat:@"%@/house?houseid=%@&secret_key=%@", STRING_REQUEST_PHONGDEP_ROOT, house_id, STRING_REQUEST_SERCRET_KEY]

// LOGIN, REQUEST
#define STRING_REQUEST_KEY_USER_NAME                                    @"user_name"
#define STRING_REQUEST_KEY_PASSWORD                                     @"password"
#define STRING_REQUEST_KEY_FK_USER                                      @"fkUser"
#define STRING_REQUEST_KEY_LATITUDE                                     @"latitude"
#define STRING_REQUEST_KEY_LONGITUDE                                    @"longitude"
#define STRING_REQUEST_KEY_SESSION_ID                                   @"sessionID"
#define STRING_REQUEST_KEY_SESSION                                      @"session"
#define STRING_REQUEST_KEY_GPS                                          @"gps"
#define STRING_REQUEST_KEY_F_NAME										@"fname"
#define STRING_REQUEST_KEY_L_NAME										@"lname"
#define STRING_REQUEST_KEY_EMAIL										@"email"
#define STRING_REQUEST_KEY_FULL_NAME                                    @"fullName"
#define STRING_REQUEST_KEY_PROFILE_NAME                                 @"profilename"
#define STRING_REQUEST_KEY_GENDER										@"gender"
#define STRING_REQUEST_KEY_ABOUT										@"about"
#define STRING_REQUEST_KEY_FK_KARD										@"fkKard"
#define STRING_REQUEST_KEY_USER_NAME_EMAIL                              @"usernameemail"
#define STRING_REQUEST_KEY_AGE                                          @"age"
#define STRING_REQUEST_KEY_LOC                                          @"loc"

#define STRING_REQUEST_KEY_NAME                                         @"name"
#define STRING_REQUEST_KEY_EMAIL                                        @"email"
#define STRING_REQUEST_KEY_USER                                         @"user"
#define STRING_REQUEST_KEY_PASS                                         @"pass"
#define STRING_REQUEST_KEY_COUNTRY_CODE                                 @"country_code"
#define STRING_REQUEST_KEY_USER_TYPE                                    @"usertype"
#define STRING_REQUEST_KEY_MESSAGE                                      @"messages"
#define STRING_REQUEST_KEY_NUMBER                                       @"number"
#define STRING_REQUEST_KEY_SEAT                                         @"seat"

#define STRING_REQUEST_KEY_USER_COUNTRY_CODE                            @"user_country_code"

// UPDATE LOCATION, REQUEST
#define STRING_REQUEST_KEY_CURRENT_LOCATION                             @"loc"
#define STRING_REQUEST_KEY_CLIENT_ID                                    @"clientid"
#define STRING_REQUEST_KEY_PRICE                                        @"price"

// UPDATE USER: USER ID, DEVICE UDID, DEVICE TOKEN, NOTIFICATION CENTER
#define STRING_REQUEST_KEY_DEVICE_TYPE                                  @"device_type"
#define STRING_REQUEST_KEY_DEVICE_NAME                                  @"device_name"
#define STRING_REQUEST_KEY_OLD_DEVICE_TOKEN                             @"old_device_token"
#define STRING_REQUEST_KEY_NEW_DEVICE_TOKEN                             @"new_device_token"
#define STRING_REQUEST_KEY_DEVICE_TOKEN                                 @"device_token"
#define STRING_REQUEST_KEY_DEVICE_UDID                                  @"device_udid"
#define STRING_REQUEST_KEY_USER_ID                                      @"user_id"
#define STRING_REQUEST_KEY_NOTIFY_ME                                    @"notify_me"
#define STRING_REQUEST_KEY_WORLD_FREE                                   @"world_free"
#define STRING_REQUEST_KEY_WORLD_FULL                                   @"world_full"
#define STRING_REQUEST_KEY_VN_FREE                                      @"vn_free"
#define STRING_REQUEST_KEY_VN_FULL                                      @"vn_full"

// PHONGDEP
#define STRING_REQUEST_KEY_EMAIL_NAME                                   @"email_name"
#define STRING_REQUEST_KEY_URL_NAME                                     @"url_name"
#define STRING_REQUEST_KEY_PHONE_FROM_NUMBER                            @"phone_from_number"
#define STRING_REQUEST_KEY_PHONE_TO_NUMBER                              @"phone_to_number"
#define STRING_REQUEST_KEY_ROOM_ID                                      @"room_id"
#define STRING_REQUEST_KEY_ROOM_NAME                                    @"room_name"

// ****************************************RESPONSE*************************************
// LOGIN, RESPONSE
#define STRING_RESPONSE_KEY_STATUS                                      @"status"
#define STRING_RESPONSE_KEY_RESULT                                      @"result"
#define STRING_RESPONSE_KEY_SESSION_ID                                  @"sessionID"
#define STRING_RESPONSE_KEY__ID                                         @"_id"
#define STRING_RESPONSE_KEY_USER                                        @"user"
#define STRING_RESPONSE_KEY_USER_NAME                                   @"user_name"
#define STRING_RESPONSE_KEY_MESSAGE_LOGIN                               @"messageLogin"
#define STRING_RESPONSE_KEY_F_NAME_LOGIN                                @"fname"
#define STRING_RESPONSE_KEY_L_NAME_LOGIN                                @"lname"
#define STRING_RESPONSE_KEY_FULL_NAME_LOGIN                             @"fullName"
#define STRING_RESPONSE_KEY_SORT_MY_KARD                                @"sortMyKard"
#define STRING_RESPONSE_KEY_SHOW_UNREAD                                 @"showUnread"
#define STRING_RESPONSE_KEY_USER_VISIBLE                                @"userVisible"
#define STRING_RESPONSE_KEY_PASSWORD                                    @"password"
#define STRING_RESPONSE_KEY_POINT_USER                                  @"pointUser"
#define STRING_RESPONSE_KEY_MSG                                         @"msg"
#define STRING_RESPONSE_KEY_OK                                          @"ok"
#define STRING_RESPONSE_KEY_NAME                                        @"name"
#define STRING_RESPONSE_KEY_EMAIL                                       @"email"
#define STRING_RESPONSE_KEY_MESSAGE                                     @"message"
#define STRING_RESPONSE_KEY_DATA                                        @"data"

// NOTIFICATION
#define STRING_RESPONSE_KEY_APS                                         @"aps"
#define STRING_RESPONSE_KEY_ALERT                                       @"alert"
#define STRING_RESPONSE_KEY_SOUND                                       @"sound"
#define STRING_RESPONSE_KEY_NOTIFICATION_TYPE                           @"notification_type"

// PHONGDEP
#define STRING_RESPONSE_KEY_ROOM_ID                                     @"roomid"
#define STRING_RESPONSE_KEY_HOUSE_ID                                    @"houseid"
#define STRING_RESPONSE_KEY_URL                                         @"url"
#define STRING_RESPONSE_KEY_IMAGE                                       @"image"
#define STRING_RESPONSE_KEY_AREA                                        @"area"
#define STRING_RESPONSE_KEY_PRICE                                       @"price"
#define STRING_RESPONSE_KEY_ADDRESS                                     @"address"
#define STRING_RESPONSE_KEY_DISTRICT                                    @"district"
#define STRING_RESPONSE_KEY_CITY                                        @"city"
#define STRING_RESPONSE_KEY_LOCATION                                    @"location"
#define STRING_RESPONSE_KEY_LATITUDE                                    @"latitude"
#define STRING_RESPONSE_KEY_LONGITUDE                                   @"longitude"

#define STRING_RESPONSE_KEY_IMAGES                                      @"images"

// USER: UPDATE USER ID, DEVICE UDID, DEVICE TOKEN, NOTIFICATION CENTER
#define STRING_RESPONSE_KEY_DEVICE_TOKEN                                @"device_token"
#define STRING_RESPONSE_KEY_DEVICE_UDID                                 @"device_udid"
#define STRING_RESPONSE_KEY_USER_ID                                     @"user_id"
#define STRING_RESPONSE_KEY_NOTIFY_ME                                   @"notify_me"

#define STRING_COUNTRY_THE_WORLD_CODE                                   @"ALL"
#define STRING_COUNTRY_THE_WORLD_NAME                                   @"The World"

// COUNTRY
#define STRING_RESPONSE_KEY_ID                                          @"id"
#define STRING_RESPONSE_KEY_CODE                                        @"code"

// BANNER ADS
#define STRING_RESPONSE_KEY_BANNER_IMAGE_URL                            @"adsUrl"
#define STRING_RESPONSE_KEY_BANNER_ADS_ID                               @"adsId"

// FB
#define STRING_RESPONSE_KEY_FB_NAME                                     @"name"
#define STRING_RESPONSE_KEY_FB_ID                                       @"id"
#define STRING_RESPONSE_KEY_FB_PICTURE                                  @"picture"
#define STRING_RESPONSE_KEY_FB_F_NAME									@"first_name"
#define STRING_RESPONSE_KEY_FB_L_NAME									@"last_name"
#define	STRING_RESPONSE_KEY_FB_EMAIL									@"email"
#define	STRING_RESPONSE_KEY_FB_GENDER									@"gender"
#define	STRING_RESPONSE_KEY_FB_ABOUT									@"about"
#define	STRING_RESPONSE_KEY_FB_BIRTHDAY									@"birthday"
#define	STRING_RESPONSE_KEY_FB_UPDATETIME								@"updated_time"

// TW
#define STRING_RESPONSE_KEY_TW_NAME                                     @"name"
#define STRING_RESPONSE_KEY_TW_GENDER                                   @"gender"
#define STRING_RESPONSE_KEY_TW_ABOUT                                    @"about"

// DATA
#define STRING_REQUEST_KEY_CONTENT_TYPE_IMAGE_JPEG                      @"image/jpeg"
#define STRING_REQUEST_KEY_FILE                                         @"file"

// OTHERS
#define INT_RESPONSE_CODE_FINISH                                        200
#define INT_RESPONSE_CODE_CONNECTION_ERROR                              1000
#define STRING_RESPONSE_CODE_SUCCESS                                    @"SUCCESS"
#define STRING_RESPONSE_CODE_FAILER                                     @"FAILER"
#define STRING_RESPONSE_CODE_EXIST                                      @"EXIST"
#define STRING_RESPONSE_CODE_ACCEPTED                                   @"ACCEPTED"
#define STRING_RESPONSE_CODE_NOTALLOW                                   @"NOTALLOW"

#define RELOAD_FEEDS                                                    @"reload"
#define MAIL_NOT_SEND                                                   @"Email not send."

#define STRING_ALERT_CONNECTION_ERROR_TITLE                             @"Offline"
#define STRING_ALERT_CONNECTION_ERROR									@"Sorry, this function is not available in offline mode.\nPlease connect to the internet to perform this function"
#define STRING_ALERT_SERVER_ERROR                                       @"We are sorry, the server is temporarily offline for an upgrade...\nWe will be back within the next few minutes. Please try again"

#define STRING_ALERT_DATA_IS_NIL										@"DATA is nil"

// MESSAGE
#define STRING_RESPONSE_KEY_SUCCESS                                     @"SUCCESS"
#define STRING_RESPONSE_KEY_INVALID                                     @"INVALID"
#define STRING_RESPONSE_KEY_FAILURE                                     @"FAILURE"
#define STRING_RESPONSE_KEY_EMPTY                                       @"EMPTY"

#define INT_PAGE_INDEX_DEFAULT                                          1
#define INT_PAGE_SIZE_DEFAULT                                           100

#define INT_LIMIT_DEFAULT                                               100
#define INT_OFFSET_DEFAULT                                              0

@protocol APIRequesterProtocol <NSObject>

@optional

- (void)requestFailed:(ASIHTTPRequest *)request andType:(ENUM_API_REQUEST_TYPE)type;
- (void)requestFinished:(ASIHTTPRequest *)request andType:(ENUM_API_REQUEST_TYPE)type;
- (void)requestTimeoutWithType:(ENUM_API_REQUEST_TYPE)type;
- (void)requestRetried:(ENUM_API_REQUEST_TYPE)type;
- (void)requestUploadingProgress:(float)progress; //Qasim 17/12/2012
- (void)queueFinished:(ASINetworkQueue *)queue; // Trong Vu
@end

@interface APIRequester : NSObject <ASIHTTPRequestDelegate, AppViewControllerProtocol, UIAlertViewDelegate,ASIProgressDelegate>
{
    ENUM_API_REQUEST_TYPE                               m_RequestType;
    ASIHTTPRequest                                      *m_ASIRequest;
    
    id<APIRequesterProtocol>                              m_Delegate;
    
    ENUM_API_REQUESTER_STEP                             m_RequestStep;
    CFAbsoluteTime                                      m_TimerRequest;
    UIAlertView                                         *m_AlertFail;
    NSString                                            *m_StringURL;
    
    ASIFormDataRequest                                  *m_ASIFormRequest;
    NSMutableDictionary                                 *m_ParamsDic;
    ASINetworkQueue                                     *m_networkQueue;
}

- (void)requestWithType:(ENUM_API_REQUEST_TYPE)type andURL:(NSString *)url andDelegate:(id)delegate;
- (void)requestWithType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL andPostMethodKind:(BOOL)methodKind andParams:(NSMutableDictionary *)params andDelegate:(id)delegate;
- (void)requestWithType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL andPostMethodKind:(BOOL)methodKind andParams:(NSMutableDictionary *)params andImageData:(NSData *)data andDelegate:(id)delegate;
- (void)requestMultiPartRequestType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL
                  andPostMethodKind:(BOOL)methodKind
                          andParams:(NSArray *)params
                            andKeys:(NSArray*)keysArr
                        andDelegate:(id)delegate;

- (void)videoRequestMultiPartRequestType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL
                       andPostMethodKind:(BOOL)methodKind
                               andParams:(NSArray *)params
                                 andKeys:(NSArray*)keysArr
                             andDelegate:(id)delegate;

- (void)requestSimpleRequestType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL
               andPostMethodKind:(BOOL)methodKind
                       andParams:(NSArray *)params
                         andKeys:(NSArray*)keysArr
                     andDelegate:(id)delegate;

- (void)videoRequestMultiPartRequestType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL
                       andPostMethodKind:(BOOL)methodKind
                               andParams:(NSArray *)params
                                 andKeys:(NSArray*)keysArr
                             andDelegate:(id)delegate ProgressBar:(UIProgressView*)progressBar andTimeOut:(int)timeout;

- (void)cancelRequest;

//Trong Vu
- (void)queueWithType:(ENUM_API_REQUEST_TYPE)type andRootURL:(NSString *)rootURL andPostMethodKind:(BOOL)methodKind andParams:(NSMutableDictionary *)params andDelegate:(id)delegate;
- (void)startQueue;
//- (void)uploadToAS3Job:(ENUM_API_REQUEST_TYPE)type andPathName:(NSString *)pathName andFileName:(NSString *)fileName andData:(NSData *)objectData andTimeOut:(int)timeout andDelegate:(id)delegate;

+ (APIRequester *)Shared;
@end
